package rensoft.portfolio.client.lib.view;

import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Image;

import static cs.java.lang.Lang.set;



import rensoft.gwt.client.view.View;

public class LinkButton extends View<Anchor> {

  private Image image;
  private final ButtonBehavior button;

  public LinkButton() {
    initWidget(new Anchor());
    button = new ButtonBehavior(this);
  }

  public ButtonBehavior asButton() {
    return button;
  }

  public Image getImage() {
    return image;
  }

  public void setImage(ImageResource imageResource) {
    image = new Image(imageResource);
    widget().getElement().appendChild(image.getElement());
  }

  public void setTarget(String target) {
    String url = Window.Location.getHref().split("#")[0];
    if (set(target))
      widget().setHref(url + "#!" + target);
    else widget().setHref(url + "#");
  }

  public void setText(String value) {
    widget().setText(value.trim());
  }

}
